package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@RestController
@Api(tags = "菜品管理的接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("添加菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);
        String key = "dish_" + dishDTO.getCategoryId();
        clean(key);
        return Result.success();
    }

    /**
     * 菜品的分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品的分页查询")
    @GetMapping("/page")
    public Result pageDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品的分页查询，查询内容{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result DeleteDish(@RequestParam List<Long> ids) {
        log.info("需要删除{}", ids);
        int delete = dishService.delete(ids);

        if (delete == StatusConstant.HAVING) {
            return Result.error("套餐中仍有要删除的菜品，不能删除");
        }
        if (delete == StatusConstant.ON_SALE) {
            return Result.error("删除的菜品中有，起售状态的");
        }
        clean("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品的口味和菜品的数据用于数据回写
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishDTO> dishById(@PathVariable Integer id) {
        log.info("查询id为{}的菜品数据", id);
        DishDTO dishDTO = dishService.selectById(id);
        return Result.success(dishDTO);
    }

    /**
     * 用于修改菜品的内容
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品{}",dishDTO);
        dishService.update(dishDTO);
        String key = "dish_" + dishDTO.getCategoryId();
        clean(key);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result ifSale(@PathVariable Integer status ,Long id){
        dishService.ifSale(status,id);
        clean("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id来查询
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result selectByCategory(Integer categoryId){
        List<Dish> dishes = dishService.selectByCategoryId(categoryId);
        return Result.success(dishes);
    }

    private void clean(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
